<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {
     function __construct(){
        parent::__construct();
      }
      
/*
 * inicio de sesion
 */
      public function index(){
          $data['css'] = array('style/login') ;
          $data['title'] = 'Inicio de sesion.';
          $data['content'] = $this->load->view('admin/v_login','',TRUE);
          $this->load->view('template/template',$data);
          }
          
          
/*
* verifica usuario logeado
*/
      public function authenticate_user(){
          $username = $this->input->post('username',TRUE);
          $userpass = $this->input->post('userpass',TRUE);
          $this->load->model('usuarios_m');
          $login = $this->usuarios_m->validate_user($username,$userpass);
          if ($login == TRUE){
              
              $data_usr = $this->level_usr($username, $userpass);
              $id_user = $data_usr->id_user;
              $level = $data_usr->desc_level;
              
              $datos_session = array(
                  'is_logued_in'=>TRUE,
                  'id_user' => $id_user,
                  'level' => $level,
                  );
              $this->session->set_userdata($datos_session);
              
              switch ($level){
                  case 'Superadministrador':
                      redirect('clientesucursal/list_clie');
                      break;
                  case 'Administrador':
                      redirect('usuario');
                      break;
                  default:
                      redirect('login');
                  }
          }else{
              redirect('login'); 
           }
      }
      
      /*
       * Control de level de usuario
       */
      public function level_usr($username, $userpass){
          $this->load->model('usuarios_m');
          return $data_level = $this->usuarios_m->level_user($username,$userpass);
      }

      /*
       * Comprueba que existe la sucursal
       */
      public function sucursal_control(){
          $this->load->model('front_m');
          $this->session->unset_userdata('sucursal');
          $codigo_suc = $this->input->post('id_sucursal',TRUE);
          $id_suc = $this->front_m->validate_sucursal($codigo_suc);
          if(!empty($id_suc)){
              $id = $id_suc->id_suc;
              $this->session->set_userdata('sucursal', $id);
                $error = $this->session->userdata('error');
                if(!empty($error)): $this->session->unset_userdata('error');endif;
              redirect('front/index_poll');
            }else{
                $this->session->set_userdata('error', 'Código Incorrecto');
                redirect('inicio');
            }
          }      
          
/*
 * Salida del sistema         
 */
      public function logout(){
          $this->session->unset_userdata(array('is_logued_in'));
          $this->session->sess_destroy();
          redirect('login');            
      }

      
      
      
}//Fin de clase Login.php
          
          

